<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006-2014 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 麦当苗儿 <zuojiazi@vip.qq.com> <http://www.zjzit.cn>
// +----------------------------------------------------------------------

namespace Home\Controller;

use Think\Controller;

class ExamineController extends Controller{
    public function index(){
        $xm = $_GET["xm"];
        if ($xm!=""){
            $date1 = date("Y-m-d");
            $date2 = date("Y-m-d");
            $list = D("sync_examinecheck")->where("xm='".$xm."'")->select();

        } else {

            $date1 = $_GET["tjrq1"];
            if ($date1==""){
                $date1 = date("Y-m-d");
            }
            $date2 = $_GET["tjrq2"];
            if ($date2==""){
                $date2 = date("Y-m-d");
            }
            $list = D("sync_examinecheck")->where("tjrq between '".$date1."' and '".$date2."'")->order("tjrq")->select();
        }
        $this->assign('tjrq1', $date1);
        $this->assign('tjrq2', $date2);
        $this->assign('list', $list);
        $this->display();

    }

    public function export(){  //输出一个excel

        $date1 = $_GET["tjrq1"];
        if ($date1==""){
            $date1 = date("Y-m-d");
        }
        $date2 = $_GET["tjrq2"];
        if ($date2==""){
            $date2 = date("Y-m-d");
        }

        $list = D("sync_examinecheck")->where("tjrq between '".$date1."' and '".$date2."'")->select();

        //1.引入phpExcel类
        import("Util.PHPExcel");
        $objExcel = new \PHPExcel();
        $cellName = array('A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z',
            'AA','AB','AC','AD','AE','AF','AG','AH','AI','AJ','AK','AL','AM','AN','AO','AP','AQ','AR','AS','AT','AU','AV','AW','AX','AY','AZ');

        $objExcel->getProperties()->setCreator("JAMES")
            ->setLastModifiedBy("JAMES")
            ->setTitle("测试一下Title")
            ->setSubject("测试Subject")
            ->setDescription("测试Description")
            ->setKeywords("测试keyword")
            ->setCategory("测试Category");
        $objExcel->setActiveSheetIndex(0);

//	  for($i=0;$i<$cellNum;$i++){
//            $objPHPExcel->setActiveSheetIndex(0)->setCellValue($cellName[$i].'2', $expCellName[$i][1]); 
//        } // 
        // ->setCellValue('A1',"Hello")
        // ->setCellValue('A2',"World")
        // ->setCellValue('C1',"中文")
        // ->setCellValue('D2',"你好");

        $objExcel->getActiveSheet()->setCellValue("A1","体检日期");
        $objExcel->getActiveSheet()->setCellValue("B1","姓名");
        $objExcel->getActiveSheet()->setCellValue("C1","身份证号");
        $objExcel->getActiveSheet()->setCellValue("D1","工作单位");
        $objExcel->getActiveSheet()->setCellValue("E1","一般");
        $objExcel->getActiveSheet()->setCellValue("F1","内科");
        $objExcel->getActiveSheet()->setCellValue("G1","外科");
        $objExcel->getActiveSheet()->setCellValue("H1","胸透");
        $objExcel->getActiveSheet()->setCellValue("I1","心电图");
        $objExcel->getActiveSheet()->setCellValue("J1","化验");
        $objExcel->getActiveSheet()->setCellValue("K1","妇科");
        $objExcel->getActiveSheet()->setCellValue("L1","体检结果");

        for($i=0;$i<count($list);$i++){
            $objExcel->getActiveSheet()->setCellValue('A'.($i+2),$list[$i]["tjrq"]);
            $objExcel->getActiveSheet()->setCellValue("B".($i+2),$list[$i]["xm"]);
//   	     $objExcel->getActiveSheet()->setCellValue("C".($i+2),$list[$i]["sfzh"]);
            $objExcel->getActiveSheet()->setCellValueExplicit("C".($i+2),
                $list[$i]["sfzh"],
                \PHPExcel_Cell_DataType::TYPE_STRING);
            $objExcel->getActiveSheet()->setCellValue("D".($i+2),$list[$i]["address"]);
            $objExcel->getActiveSheet()->setCellValue("E".($i+2),$list[$i]["keshi1"]);
            $objExcel->getActiveSheet()->setCellValue("F".($i+2),$list[$i]["keshi2"]);
            $objExcel->getActiveSheet()->setCellValue("G".($i+2),$list[$i]["keshi3"]);
            $objExcel->getActiveSheet()->setCellValue("H".($i+2),$list[$i]["keshi4"]);
            $objExcel->getActiveSheet()->setCellValue("I".($i+2),$list[$i]["keshi5"]);
            $objExcel->getActiveSheet()->setCellValue("J".($i+2),$list[$i]["keshi6"]);
            $objExcel->getActiveSheet()->setCellValue("K".($i+2),$list[$i]["keshi7"]);
            $objExcel->getActiveSheet()->setCellValue("L".($i+2),$list[$i]["note"]);

        }



        // Redirect output to a client’s web browser (Excel5)
        header('Content-Type: application/vnd.ms-excel');
        header('Content-Disposition: attachment;filename="01simple.xls"');
        header('Cache-Control: max-age=0');
// If you're serving to IE 9, then the following may be needed
        header('Cache-Control: max-age=1');

// If you're serving to IE over SSL, then the following may be needed
        header ('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); // Date in the past
        header ('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT'); // always modified
        header ('Cache-Control: cache, must-revalidate'); // HTTP/1.1
        header ('Pragma: public'); // HTTP/1.0
        $objWriter = \PHPExcel_IOFactory::createWriter($objExcel,'Excel5');
        $objWriter->save('php://output');
        exit;


    }
}